<?php
/**
 * Copyright 2013 Marek Kaszewski
 *
 * This file is part of the JHUB1 Online Community Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Tools\Controller;

use Application\Model\Endpoint\Prototype\Integer;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use Zend\Console\Request as ConsoleRequest;
use Application\Entity\BO\UsersBO;
use Application\Entity\BO\AgentsBO;
use Application\Entity\BO\EndpointValuesBO;
use Doctrine\ORM\Query\ResultSetMapping;
use Application\Entity\BO\EndpointTypesBO;

class IndexController extends AbstractActionController
{
    public function resetpasswordAction()
    {
        $request = $this->getRequest();
        if (!$request instanceof ConsoleRequest){
            throw new \RuntimeException('You can only use this action from a console!');
        }
        echo "sdsadadas";
    }

    public function dboperationsAction()
    {
        $request = $this->getRequest();
        if (!$request instanceof ConsoleRequest){
            throw new \RuntimeException('You can only use this action from a console!');
        }
        $operation = $request->getParam('operation');
        $isVerbose = $request->getParam('verbose',false) || $request->getParam('v',false);
        if($isVerbose) {
            echo "Performing database operation: $operation \n";
        }
        switch ($operation) {
            case 'drop':
                $ag = new AgentsBO($this->getServiceLocator());

                $agents = array('tmgguhxrv6','r6r7a42ng8');
                $epv = new EndpointValuesBO($this->getServiceLocator());
                $res = $epv->checkForNewEndpoints($agents);
                var_dump($res);
             /*   $rsm = new ResultSetMapping;
                $sl = $this->getServiceLocator();
                $em = $sl->get('Doctrine\ORM\EntityManager');
                $query = $em->createNativeQuery('DROP TABLE jhub1onlineapp.jhoEndpointValues', $rsm);
                $query->getResult();*/
                exit;
            case 'init':
                $userBO = new UsersBO($this->getServiceLocator());
                $formData['email'] = "mmmddeeg@qaba.com";
                $formData['password'] = "1qaz2wsx";
                $formData['status'] = 1;
                $existUserPO = $userBO->getUserByEmail($formData['email']);
                if (null == $existUserPO) {
                    $newUserPO = $userBO->insertUser($formData);
                    if($isVerbose) {
                        echo "New user added.\n";
                    }
                    $this->createAgents($newUserPO);
                    $this->generateEndpointTypes($newUserPO);
                } else {
                    if($isVerbose) {
                    echo "User already exists in database.\n";
                    }
                }
                exit;
        }

    }

    public function donowAction()
    {
        echo "sdfsf";
    }

    private function createAgents($userPO)
    {
        $agentsBO = new AgentsBO($this->getServiceLocator());
        $agentPO = $agentsBO->addAgent($userPO, "agent1", "auto generated agent 1");
        $this->generateValues($agentPO, "eee");

        $agentPO = $agentsBO->addAgent($userPO, "agent2", "auto generated agent 2");
        $this->generateValues($agentPO, "sed");

    }

    private function generateValues($agentPO, $pre)
    {
        $endpointValuesBO = new EndpointValuesBO($this->getServiceLocator());
        sleep(2);
        $endpointValuesBO->insertValue($agentPO, "temp" . $pre, "44");
        sleep(2);
        $endpointValuesBO->insertValue($agentPO, "temp" . $pre, "45");
        $endpointValuesBO->insertValue($agentPO, "temp2" . $pre, "49");
        sleep(2);
        $endpointValuesBO->insertValue($agentPO, "temp" . $pre, "46");
        $endpointValuesBO->insertValue($agentPO, "temp2" . $pre, "47");
        sleep(2);
        $endpointValuesBO->insertValue($agentPO, "temp" . $pre, "47");
        $endpointValuesBO->insertValue($agentPO, "temp2" . $pre, "46");
    }

    private function generateEndpointTypes($userPO)
    {
        $types = new EndpointTypesBO($this->getServiceLocator());
        $integerPrototype = new Integer();
        $types->insertValue($userPO, "MyIntegerType", "Automatically populated test type", "Integer", $integerPrototype);
    }
}
